package com.tanhua.server.service;

import com.tanhua.dubbo.api.DimensionsApi;
import com.tanhua.dubbo.api.TestSoulApi;
import com.tanhua.model.LinHun.*;
import com.tanhua.model.vo.*;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class TestSoulService {
    @DubboReference
    private TestSoulApi testSoulApi;

    @DubboReference
    private DimensionsApi dimensionsApi;

    //查询问卷
    public List<SoulVO> testSoul() {
        //查询所有数据
        List<Soul> list = testSoulApi.findAll();
        //遍历数据
        //卷子vo
        List<SoulVO> voList = new ArrayList<>();
        //题vo
        for (Soul soul : list) {
                //创建卷子vo对象
                SoulVO soulVO = new SoulVO();
                //将实体类赋值到vo
                BeanUtils.copyProperties(soul, soulVO);
                //由于页面需要卷子编号，单独设置id
                soulVO.setId(soul.getSid());
                //获得卷子编号id
                String sid = soul.getSid();
                //用试题id查询题库表
                List<Questions> questionsList = testSoulApi.findBySId(sid);
                //遍历题目
                List<QuestionsVo> list1 = new ArrayList<>();
                for (Questions questions : questionsList) {
                    //获得题目id
                    QuestionsVo questionsVo = new QuestionsVo();
                    BeanUtils.copyProperties(questions, questionsVo);
                    questionsVo.setId(questions.getQid());
                    //获取题的编号
                    String qid = questions.getQid();
                    //根据题目Id和卷子id获取每道题的选项
                    List<Options> optionsList = testSoulApi.findByQidSid(qid, sid);
                    //封装选项
                    List<OptionsVo> list2 = new ArrayList<>();
                    for (Options options : optionsList) {
                        OptionsVo optionsVo = new OptionsVo();
                        BeanUtils.copyProperties(options, optionsVo);
                        optionsVo.setId(options.getOid());
                        list2.add(optionsVo);
                    }
                    //封装到题
                    questionsVo.setOptions(list2);
                    list1.add(questionsVo);
                }
                //封装到卷子
                //soulVO.setReportId(String.valueOf(UserHolder.getUserId()));
                soulVO.setQuestions(list1);
                //将卷子vo转化为集合
                voList.add(soulVO);
        }
        //将对象存入集合
        return voList;
    }

    //提交问卷
    public String tiesSoul(List<Suo> list) {
        //缘分值
        Integer zongfen = 0;
        //获得页面数据
        for (Suo suo : list) {

            String optionId = suo.getOptionId();//选项编号
            String questionId = suo.getQuestionId();//试题编号
            //查询分数
            String fen = testSoulApi.findByAnswers(optionId,questionId);
            zongfen+=Integer.valueOf(fen);
        }
        //查询报告相似度
        Xinge xingeList = testSoulApi.findByXingen(zongfen);
        Baogao baogao = new Baogao();
        if (xingeList!=null) {
            baogao.setReportId(String.valueOf(UserHolder.getUserId()));
            baogao.setConclusion(xingeList.getConclusion());
            baogao.setCover(xingeList.getCover());
            baogao.setSore(xingeList.getSore());
            baogao.setWdu(xingeList.getWdu());
            baogao.setXiang(xingeList.getXiang());
            testSoulApi.save(baogao);
        }
        return baogao.getReportId();
    }

    //查看报告
    public BaogaoVo report(String id) {
       Baogao baogao = testSoulApi.findBy(id);
        BaogaoVo baogaoVo = new BaogaoVo();
        if (baogao!=null) {
            baogaoVo.setConclusion(baogao.getConclusion());
            baogaoVo.setCover(baogao.getCover());

            //设置维度值
            List<Dimensions> dimensionsList = dimensionsApi.findAll();
            ArrayList<DimensionsVo> dimensionsVos = new ArrayList<>();
            for (Dimensions dimensions : dimensionsList) {
                DimensionsVo dimensionsVo = new DimensionsVo();
                dimensionsVo.setKey(dimensions.getKeyStr());
                dimensionsVo.setValue(dimensions.getValueStr());
                dimensionsVos.add(dimensionsVo);
            }
            baogaoVo.setDimensions(dimensionsVos);

            Map<String, Object> map1 = new HashMap<>();
            map1.put("id",1);
            map1.put("avatar","https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/test_soul/fox.png");
            List<Map<String, Object>> mapArrayList1 = new ArrayList<Map<String, Object>>();
            mapArrayList1.add(map1);
            baogaoVo.setSimilarYou(mapArrayList1);
        }
        return baogaoVo;
    }
}
